import WeflyAxios from "./WeflyAxios.js";
import { baseRequest, baseResponse, downRequest, downResponse } from "./middlewares";

const defaultConfig = {
  withCredentials: false,
};
const weflyAxios = new WeflyAxios(defaultConfig);

weflyAxios
  .useRequest((_, next) => {
    next();
  })
  .useRequest(downRequest)
  .useRequest(baseRequest);

weflyAxios
  .useResponse((response, next) => {
    const data = response.data;
    if (response.status === 401) {
      return Promise.reject(data);
    }
    next();
  })
  .useResponse(downResponse)
  .useResponse(baseResponse);

/**
 * 将request包裹在实例中调用，防止this指向被修改
 * @param args
 * @property { import('@root/request/BaseAxios').AxiosRequestConfig } axiosConfig axios配置
 * @property { import('@root/request/BaseAxios').CustomConfig } customConfig 自定义配置，必须以 多单词+下划线 的形式命名属性
 * @returns { Promise<T> }
 */
export default (...args) => weflyAxios.request(...args);
